import { Notification, Message, Loading } from 'element-ui'

import { Commit, Dispatch } from 'vuex'
import { get, defaultsDeep, cloneDeep } from 'lodash'
import dayjs from 'dayjs'

import { getAttendanceReady, addAttendance, deleteAttendance, reviewAttendance, getAttendanceList } from '@/api/attendance'

export interface State { }

const state: State = {
  search: {
    workerId: '',
    jobNumber: '',
    workerName: '',
    beginTime: '',
    endTime: '',
    page: 1,
    pageSize: 20
  },
  recordList: [],
  totalRecord: 0,
  workerList: [
    {
      workerName: '张三',
      workerId: '1'
    },
    {
      workerName: '历史',
      workerId: '2'
    }
  ]
}

const getters = {}

const mutations = {
  SET_SEARCH(state: any, payload: any) {
    state.search = payload
  },
  SET_ATTENDANCE_LIST(state: any, payload: any) {
    state.recordList = payload
  },
  SET_WORK_LIST(state: any, payload: any) {
    state.workerList = payload
  },
  SET_TOTAL_RECORD(state: any, payload: any) {
    state.totalRecord = payload
  }
}

const actions = {
  //表单数据更新
  COMMIT_TABLE_DATA({ commit, state }: any) {
    let params = state.search

    getAttendanceList(params).then(resp => {
      let recordList = get(resp, 'data', [])
      commit('SET_ATTENDANCE_LIST', recordList)
    })
  },

  //6-01-a PC新增准备
  async GET_ATTENDANCE_READY({ state, commit, rootState, dispatch }: any, payload: any) {
    let resp = await getAttendanceReady(payload).catch((err: any) => {
      Notification.error(err)
    })
    if (resp) {
      let workerList = get(resp, 'workerList', [])
      commit('SET_WORK_LIST', workerList)

      //   Message.success('请求成功!')
      console.log(resp)
      const data = resp
      return data
    }
  },
  //6-01-b 考勤记录添加
  async ADD_ATTENDANCE({ state, commit, rootState, dispatch }: any, payload: any) {
    let resp = await addAttendance(payload).catch((err: any) => {
      Notification.error(err)
    })
    if (resp) {
      Message.success('请求成功!')
      dispatch('COMMIT_TABLE_DATA')
      return true
    }
  },
  //6-02 考勤记录删除
  async DETELE_ATTENDANCE({ state, commit, rootState, dispatch }: any, payload: any) {
    let resp = await deleteAttendance(payload).catch((err: any) => {
      Notification.error(err)
    })
    if (resp) {
      Message.success('请求成功!')
      dispatch('COMMIT_TABLE_DATA')
      return true
    }
  },
  //6-04 考勤记录查询/进入
  async GET_ATTENDANCE_LIST({ state, commit, rootState, dispatch }: any, payload: any) {
    commit('SET_SEARCH', payload)
    console.log('cdhervfh', payload)
    let resp = await getAttendanceList(payload).catch((err: any) => {
      Notification.error(err)
    })
    if (resp) {
      let recordList = get(resp, 'data', [])
      commit('SET_ATTENDANCE_LIST', recordList)
      let total = get(resp, 'totalRecord', 0)
      commit('SET_TOTAL_RECORD', total)
      return true
    }
  },
  //6-03 考勤审核
  async REVIEW_ATTENDANCE({ state, commit, rootState, dispatch }: any, payload: any) {
    let resp = await reviewAttendance(payload).catch((err: any) => {
      Notification.error(err)
    })
    if (resp) {
      Message.success('提交成功!')
      dispatch('COMMIT_TABLE_DATA')
      console.log(resp)

      return true
    }
  }
}

export default {
  namespaced: true,
  state,
  mutations,
  actions,
  getters
}
